package com.zhaopin.theswordreferstooffer;

import java.util.Stack;

/**
 * @author yin.peide
 * @date 2021-06-11 16:16
 * 面试题21：包含min函数的栈
 */
public class Offer21 {


    public static void main(String[] args) {

        MinStack minStack = new MinStack();
        minStack.push(2);
        minStack.push(3);
        minStack.push(4);
        minStack.push(5);
        System.out.println(minStack.pop());

    }

}

class MinStack {

    //主要的栈列
    private Stack<Integer> stack1 = new Stack<>();
    //辅助的栈列
    private Stack<Integer> stack2 = new Stack<>();


    public void push(int x) {
        stack1.push(x);
        if (stack2.empty() || stack2.peek() >= x) {
            stack2.push(x);
        } else {
            stack2.push(stack2.peek());
        }
    }

    public int top() {
        return stack1.peek();
    }

    public Integer pop() {
        stack1.pop();
        return stack2.pop();

    }


}
